<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <Header @addTodo="addTodo"></Header>
        <List :todos="todos"  ></List>
        <Footer :todos="todos" @clearAllTodo="clearAllTodo"></Footer>
      </div>
    </div>
  </div>
</template>

<script>

import Footer from "@/components/Footer";
import Header from "@/components/Header";
import List from "@/components/List";
// 消息订阅引入
import pubsub from "pubsub-js";

export default {
  name: "App",

  components:{
    // eslint-disable-next-line vue/no-unused-components
    Footer,
    // eslint-disable-next-line vue/no-unused-components
    Header,

    // eslint-disable-next-line vue/no-unused-components
    List
  },
   data(){
    return{
      // todos:[
      //   {id:'001',title:'抽烟',done:true},
      //   {id:'002',title:'喝酒',done:false},
      //   {id:'003',title:'烫头',done:true},
      // ],
      //读的是JSON字符串，则转对象             定义这个空数组，让组件不会报错
      todos:JSON.parse(localStorage.getItem('todos')) || []
    }
   },
  methods:{
    addTodo(todoObj){
      //todos 前方加一数据
      this.todos.unshift(todoObj)
    },
    //编辑更新
    updataTodo(id,title){
      this.todos.forEach((todo)=>{
        if(todo.id === id){
          todo.title = title
        }
      })
    },
  //  勾选or取消勾选一个todo
  //   checkTodo(id){
  //     this.todos.forEach((todo)=>{
  //       if(todo.id === id) todo.done = !todo.done
  //     })
  //   },
    //删除一个todo
    deleteTodo(_,id){
      // filter 筛选过滤
      this.todos = this.todos.filter((todo)=>{
          return todo.id !== id
      })
    },
  //  清除所有已经完成的任务
    clearAllTodo(){
      this.todos = this.todos.filter((todo)=>{
        return !todo.done
      })
    }
  },
  watch:{
    //必须要用深度监视,不用深度监视会导致监测不到勾选
    todos:{
      //本地读取是字符串形式，所以把对象转为字符串
      deep:true,
      handler(value){
        localStorage.setItem('todos',JSON.stringify(value))
      }
    }
  },
  mounted() {
    this.$bus.$on('checkTodo', (id)=>{
          this.todos.forEach((todo)=>{
            if(todo.id === id) todo.done = !todo.done
          })

    })
    // this.$bus.$on('deleteTodo',(id)=>{
    //   // filter 筛选过滤
    //   this.todos = this.todos.filter((todo)=>{
    //     return todo.id !== id
    //   })
    // })
    //订阅消息
    this.deleteid =  pubsub.subscribe('deleteTodos',
    // 一种写法
    //     (msg,id)=>{
    //     this.deleteTodo(id)
    // }
    //     另一种写法
        this.deleteTodo
    )
    //编辑
    this.$bus.$on('updata',this.updataTodo)
  },
//
  beforeDestroy() {
  this.$bus.$off('checkTodo')
    // this.$bus.$off('deleteTodo')
    //销毁订阅
    pubsub.unsubscribe(this.deleteid)

}
}
</script>

<style >
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn-edit{
  color: #fff;
  background-color: skyblue;
  border: 1px solid #365e6f;
  margin-right: 5px;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

</style>